﻿using System;

namespace CSharpConsoleTestSolution.Demo {
    /**
     * P24: 什么是类
     */
    public class P24_class {

        /**
         * 1.构造方法 & 析构器, 时间: 29:50
         */
        public static void constructor_test() {
            Console.WriteLine();
            Console.WriteLine("1.构造方法 & 析构器");
            Console.WriteLine("  空参构造, 快捷键: ctor");
            Console.WriteLine("  有参构造, 快捷键: ctorp");
            var student = new Student() {Id = 1, Name = "Timothy"};
            student.Report();
        }

        private class Student {
            public int Id { get; set; }
            public string Name { get; set; }
            
            /**
             * 空参构造, 快捷键: ctor
             */
            public Student() { }
            
            /**
             * 有参构造, 快捷键: ctorp
             */
            public Student(int id, string name) { Id = id; Name = name; }

            public void Report() {
                Console.WriteLine($"I'm {Name}, id is {Id}");
            }

            /**
             * 析构器: 当一个对象在消亡的时候，由编译器自动调用。
             */
            ~Student() {
                Console.WriteLine();
                Console.WriteLine("2.析构器: 当一个对象在消亡的时候，由编译器自动调用。");
                Console.WriteLine("  ~Student() { ... }");
                Console.WriteLine($"对象消亡: id={Id}");
            }
        }
        
        
        
        
        private class Student2 {
            public int Id { get; set; }
            public string Name { get; set; }
            public Student2() { }
            public Student2(int id, string name) { Id = id; Name = name; }
            public void Report() {
                Console.WriteLine($"I'm {Name}, id is {Id}");
            }
        }

        /**
         * 3.反射 & 动态编程(dynamic), 时间: 35:01
         */
        public static void reflect_test() {
            Console.WriteLine();
            Console.WriteLine("3.反射 & 动态编程(dynamic): Activator.CreateInstance");
            Console.WriteLine("<!-- 反射里的 dynamic, 需要如下引用 -->");
            Console.WriteLine("<Reference Include=\"Microsoft.CSharp\" />");
            Console.WriteLine("Type type = typeof(Student2);");
            Console.WriteLine("Student2 student0 = (Student2)Activator.CreateInstance(type, 1, \"Timothy\");");
            Console.WriteLine("dynamic stuDynamic = Activator.CreateInstance(type, 2, \"Tommy\"); //dynamic 动态编程, 可强制不报错地访问'方法'&'字段'");
            Console.WriteLine("Student2 student2 = Activator.CreateInstance<Student2>();");
            Console.WriteLine();
            Console.WriteLine("student0.Report();");
            Console.WriteLine("Console.WriteLine($\"I'm {stuDynamic.Name}, id is {stuDynamic.Id}\");");
            Console.WriteLine("stuDynamic.Report();");
            Console.WriteLine("student2.Report();");
            Type type = typeof(Student2);
            Student2 student0 = (Student2)Activator.CreateInstance(type, 1, "Timothy");
            dynamic stuDynamic = Activator.CreateInstance(type, 2, "Tommy");

            Student2 student2 = Activator.CreateInstance<Student2>();

            student0.Report();
            Console.WriteLine($"I'm {stuDynamic.Name}, id is {stuDynamic.Id}");
            stuDynamic.Report();
            student2.Report();
        }



        private class Student3 {
            public static int Amount{ get; set; }
            //静态构造器(必须是空参)
            static Student3() { Amount = 100; }

            public int Id { get; set; }
            public string Name { get; set; }
            public void Report() {
                Console.WriteLine($"I'm {Name}, id is {Id}");
            }

            public Student3() {
                Amount++;
            }
            ~Student3() {
                Amount--;
            }
            
        }

        /**
         * 4.静态构造器(必须是空参), 时间: 41:10
         */
        public static void static_constructor_test() {
            Console.WriteLine();
            Console.WriteLine("4.静态构造器(必须是空参)");
            Console.WriteLine("public static int Amount{ get; set; }");
            Console.WriteLine("static Student3() { Amount = 100; }  //这就是");
            var student = new Student3();
        }
    }
}